package com.zxw.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.user.domain.dto.LoginUserDTO;
import com.zxw.user.domain.entity.User;
import com.zxw.user.service.LoginService;
import com.zxw.user.service.RoleButtonService;
import com.zxw.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {


    private final UserService userService;

    private final RoleButtonService roleButtonService;

    @Override
    public LoginUserDTO getLoginUserInfo() {
        LoginUserDTO loginUserDTO = new LoginUserDTO();
        Long userId = JwtUtils.getUserId();
        User user = userService.getById(userId);
        BeanUtil.copyProperties(user, loginUserDTO);
        //获取角色
        List<String> roles = JwtUtils.getRoles();
        loginUserDTO.setRoles(roles);
        //获取按钮权限
        Map<String, Map> btnIdentifierByRoleName = roleButtonService.getRoleBtnByRoleName(roles);
        loginUserDTO.setElShowMap(btnIdentifierByRoleName);
        return loginUserDTO;
    }
}
